<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="db.DBMain"%>
<%@page import="db.tables.DBCountryTable"%>
<%@page import="util.Constant, secure.UserRights"%>
<%@page import="java.util.ArrayList"%>
<%@page import="db.tables.DBTownTable"%>
<%@page import="html.RequestParser"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
<!--
function submit(){
document.form.submit();
}
//-->
</script>
<%
	if(RequestParser.send_action_file==null)RequestParser.send_action_file="town.jsp";
	if(RequestParser.send_method==null)RequestParser.send_method="get";
	final String file_String=RequestParser.send_action_file,method_String=RequestParser.send_method;
	DBCountryTable countryTable=DBMain.getCountryTable();
	DBTownTable townTable=DBMain.getTownTable();
	ArrayList<ArrayList<Object>> towns=null;
	ArrayList<ArrayList<Object>> countries=null;
	Long town_id=null,country_id=null;
	
	town_id=RequestParser.getLongFromParameter(request,Constant.Content.TOWN);
	country_id=RequestParser.getLongFromParameter(request,Constant.Content.COUNTRY);


	Long user_id = (Long)session.getAttribute(Constant.USERID);
	boolean admin = UserRights.isUserAdmin(user_id);
	if(!admin) {
		out.println("Achtung, sie haben keine Administratorrechte!");
	}
	
	town_id=RequestParser.getLongFromParameter(request,Constant.Content.TOWN);
	country_id=RequestParser.getLongFromParameter(request,Constant.Content.COUNTRY);

	if(admin) {
		if(request.getParameter(Constant.Content.SUBMIT_COUNTRY)!=null){
			if(request.getParameter(Constant.Content.INPUT_COUNTRY)!=null&&request.getParameter(Constant.Content.INPUT_COUNTRY_SHORT)!=null){
		String country=request.getParameter(Constant.Content.INPUT_COUNTRY),country_short=request.getParameter(Constant.Content.INPUT_COUNTRY_SHORT);
		country_id=countryTable.insertGetPK(new String[]{DBCountryTable.COUNTRY_NAME_COL,DBCountryTable.COUNTRY_SHORT_COL},new Object[]{country,country_short});
			}else if(request.getParameter(Constant.Content.INPUT_COUNTRY)!=null ^ request.getParameter(Constant.Content.INPUT_COUNTRY_SHORT)!=null){
		// Error output because only one parameter is set - needed are both!
			}
		}
	}

	if(admin) {
		if(request.getParameter(Constant.Content.SUBMIT_TOWN)!=null){
			if(request.getParameter(Constant.Content.INPUT_TOWN)!=null&&request.getParameter(Constant.Content.INPUT_POSTAL_CODE)!=null
			&&country_id!=null){
		String town=request.getParameter(Constant.Content.INPUT_TOWN);
		Integer postal_code=RequestParser.getIntegerFromParameter(request,Constant.Content.INPUT_POSTAL_CODE);
		town_id=townTable.insertGetPK(new String[]{DBTownTable.TOWN_NAME_COL,DBTownTable.POSTAL_CODE_COL,DBTownTable.COUNTRY_ID_COL},
				new Object[]{town,postal_code,country_id});
			}else if(request.getParameter(Constant.Content.INPUT_TOWN)!=null ^ request.getParameter(Constant.Content.INPUT_POSTAL_CODE)!=null ^ country_id!=null){
		// Error output because only one parameter is set - needed are both!
			}
		}
	}

	if(admin) {
		if(request.getParameter(Constant.Content.DELETE_COUNTRY)!=null&&country_id!=null){
			if(countryTable.delete(DBCountryTable.COUNTRY_ID_COL,country_id)>=1)
		DBMain.getTownTable().delete(DBTownTable.COUNTRY_ID_COL,country_id);
		}
	}

	if(admin) {
		if(request.getParameter(Constant.Content.DELETE_TOWN)!=null&&town_id!=null){
			townTable.delete(DBTownTable.TOWN_ID_COL,town_id);
		}
	}

	countries = countryTable.getSelect(
		new String[]{DBCountryTable.COUNTRY_ID_COL,DBCountryTable.COUNTRY_NAME_COL,DBCountryTable.COUNTRY_SHORT_COL},
		null,null);

	towns = townTable.getSelect(
		new String[]{DBTownTable.TOWN_ID_COL,DBTownTable.TOWN_NAME_COL,DBTownTable.POSTAL_CODE_COL},
		new String[]{DBTownTable.COUNTRY_ID_COL},new Object[]{country_id});
%>
<table>
<tr>
<td>
<form name="<%=Constant.Content.COUNTRY %>" action="<%=file_String%>" method="<%=method_String %>">
	<select name="<%=Constant.Content.COUNTRY %>" onchange="submit()">
		<%
			RequestParser.printOptionList(out,countries,country_id);
		%>
	</select>
<% if(admin) { %>
	<input name="<%=Constant.Content.DELETE_COUNTRY %>" value="delete" type="submit"/>
<% } %>
</form>
<% if(admin) { %>
<form name="add_country" action="<%=file_String%>" method="<%=method_String %>">
	<table>
	<tr><td>Land</td><td>L&auml;nderk&uuml;rzel</td></tr>
	<tr><td><input name="<%=Constant.Content.INPUT_COUNTRY %>" type="text"/></td>
	<td><input name="<%=Constant.Content.INPUT_COUNTRY_SHORT %>" size="6" type="text"/></td>
	</tr></table>
	<input name="submit_country" type="submit"/>
	<input type="reset" name="Zur&uuml;cksetzen" value="Zur&uuml;cksetzen">
</form>
<% } %>
</td>
<td>
<form name="<%=Constant.Content.ADD_TOWN %>" action="<%=file_String%>" method="<%=method_String %>">
	<select name="<%=Constant.Content.TOWN %>" onchange="submit()">
		<%
			RequestParser.printOptionList(out,towns,town_id);
		%>
	</select>
<% if(admin) { %>
     <input name="<%=Constant.Content.COUNTRY%>" type="hidden" value="<%=country_id %>"/>
	<input name="<%=Constant.Content.DELETE_TOWN %>" value="delete" type="submit"/>
<% } %>
</form>
<% if(admin) { %>
<form name="<%=Constant.Content.ADD_TOWN %>" action="<%=file_String%>" method="<%=method_String %>">
	<table>
	<tr><td>Postleitzahl</td><td>Stadt</td></tr>
	<tr>
	<td><input name="<%=Constant.Content.INPUT_POSTAL_CODE %>" size="6" type="text"/></td>
	<td><input name="<%=Constant.Content.INPUT_TOWN %>" type="text"/></td>
	</tr></table>
	<input name="<%=Constant.Content.COUNTRY%>" type="hidden" value="<%=country_id %>"/>
	<input name="<%=Constant.Content.SUBMIT_TOWN %>" type="submit"/>
	<input type="reset" name="Zur&uuml;cksetzen" value="Zur&uuml;cksetzen">
</form>
<% } %>
</td>
</tr>
</table>
</body>
</html>